const app = getApp()
import { requestApi } from '../../api'
import { formatImage } from '../../utils/formatImage'

Page({
    data: {
        data: {},   // 个人中心信息
        avatar_image_uri: '',
        ordersInfo: {},  // 订单数量
        loading: true,
        channel: '',
        reservationData: [],
        resultStatusText: '',

        h: app.data.h,
        avatar_info: null,
        VIP_Info: ['铜卡会员', '银卡会员', '金卡会员', '铂金卡会员', '黑卡会员', '至尊卡会员'],
        userData: {
            coupon: "-",
            footprint: "-"
        },
        orderData: {
            created_count: 0,
            processing_count: 0,
            shipped_count: 0,
            waiting_rate_count: 0,
            refunded_count_all : 0
        }
    },
    onShow: function (options) {
        let channel = options && options.channel || 'wx_xcx';
        let userinfo = wx.getStorageSync('userinfo') || {};

        if (userinfo.mobile_phone) {
            this.getProfileInfo();
            this.getMyselfInfo();
            this.setData({
                channel: channel
            })
        }
    },
    // 获取个人主页信息
    getProfileInfo() {
        const { channel, VIP_Info } = this.data;
        // 调用网络请求
        app.request({
            url: requestApi.ProfileUrl,
                data: {
                    channel,
                    is_new_ver: true
                }
        }, (d) => {
            const info = d.original.data;
            if (d.original.ret_code == 200) {
                info.avatar_full_img = formatImage.cdnImage(d.original.data.avatar_image_uri, formatImage.THUMBNAIL_S);
                info.vip_name = info.vip_id ? VIP_Info[info.vip_id - 1] : ''
            }

            this.setData({
                avatar_info: info
            })
            // 设置存储数据
            wx.setStorage({
                key: 'profileinfo',
                data: info
            })
        },
        (error) => {
            wx.showModal({
                title: '提示',
                content: error,
                showCancel: false,
            })
        })
    },
    // 个人中心详情
    bindPersonalDetail () {
        if (!app.toLogin()) {
            return false
        }
        const { channel } = this.data;

        wx.navigateTo({
            url: '/pages/personal/personalDetail/personalDetail' + '?channel=' + channel
        })
    },
    // 优惠券
    bindCoupon () {
        if (!app.toLogin()) {
            return false
        }
        const { channel } = this.data;
        wx.navigateTo({
            url: '/pages/personal/coupon/coupon' + '?channel=' + channel,
        })
    },
    // 足迹
    bindFootprint() {
        if (!app.toLogin()) {
            return false
        }
        const { channel } = this.data;
        wx.navigateTo({
            url: '/pages/personal/footprint/footprint' + '?channel=' + channel,
        })
    },
    // 我的订单
    bindOrder(e) {
        if (!app.toLogin()) {
            return false
        }
        const { channel } = this.data;
        const { type } = e.currentTarget.dataset;

        wx.navigateTo({
            url: '/pages/trade/tradeList/tradeList?tradeStatus=' + type + '&channel=' + channel,
        })
    },
    // 收货地址
    bindAddress() {
        if (!app.toLogin()) {
            return false
        }
        
        wx.navigateTo({
            url: '/pages/personal/addressList/addressList',
        })
    },
    bindOfficial() {
        wx.navigateTo({
            url: `/pages/webView/webView?url=https://mp.weixin.qq.com/s/Ivz1qtfITQ6psbQ4qW_Gnw`,
        })
    },
    getMyselfInfo() {
        app.request({
            url: requestApi.myselfInfo,
            data: {
                is_new_ver: true
            }
        }, (d) => {
            if (d.original.ret_code == 200) {
                const info = d.original.data;
                const { userData, orderData } = this.data;

                info.item_list.forEach(item => {
                    if (item.type == 514) {
                        userData['coupon'] = item.data.item_list[0].total;
                        userData['footprint'] = item.data.item_list[1].total;
                    }

                    if (item.type == 503) {
                        for(let k in orderData) {
                            orderData[k] = item.data[k];
                        }
                    }
                })

                this.setData({
                    userData,
                    orderData
                })
            }
  
        },
        (error) => {
            wx.showModal({
                title: '提示',
                content: error,
                showCancel: false,
            })
        })
    }
})